// Sqlite3Demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "sqliteEF.h"

int main()
{
    std::cout << "start ...\n";
    //sqliteEF::init("pfile.sqlite");
    sqliteEFModel::init("pfile2.sqlite");
    sqliteEFModel userModel;

    string create_tb = "CREATE TABLE COMPANY(" \
        "ID INT PRIMARY KEY     NOT NULL," \
        "NAME           TEXT    NOT NULL," \
        "AGE            INT     NOT NULL," \
        "ADDRESS        CHAR(50)," \
        "SALARY         REAL);";
    // 直接运行脚本
    sqliteEF::GetClient()->exec(create_tb);


    // 添加数据
    sqliteEFCols c1;
    c1.add("ID", 1);
    c1.add("NAME", "name1");
    c1.add("AGE", 23);
    c1.add("ADDRESS", "addr1");
    c1.add("SALARY", 3023.21);
    userModel.table("COMPANY").insert(c1).exec();

    c1.add("ID", 2);
    c1.add("NAME", "name2");
    c1.add("AGE", 24);
    c1.add("ADDRESS", "addr2");
    c1.add("SALARY", 3023.21);
    userModel.table("COMPANY").insert(c1).exec();

    c1.add("ID", 3);
    c1.add("NAME", "name2");
    c1.add("AGE", 25);
    c1.add("ADDRESS", "addr2");
    c1.add("SALARY", 3023.21);
    userModel.table("COMPANY").insert(c1).exec();

    sqliteEFInPars squery;
    squery.add(1);
    squery.add(23);

    // 带参数查询（参数检查特殊符号，防注入）
    sqliteEFRows userComs = userModel.table("COMPANY").select("ID,AGE,NAME").where("ID > ? and AGE > ?").findAlls(squery);
    sqliteEFModel::outStr(userComs);

    squery.clear();
    squery.add("name3");
    squery.add("addr3");


    // 带参数更新数据（参数检查特殊符号，防注入）
    userModel.table("COMPANY").update("NAME=?,ADDRESS=?").where("ID = 2").exec(squery);

    // 只使用 object userComs 里面的 ID 一项来查询
    userComs = userModel.table("COMPANY").select().where(userComs[0],"ID").findAlls();
    sqliteEFModel::outStr(userComs);

    userComs[0].get("ADDRESS")->str_val = "addr4----";
    userModel.table("COMPANY").update(userComs[0],"ADDRESS").where("ID = 2").exec();

    userComs = userModel.table("COMPANY").select().where(userComs[0], "ID").findAlls();
    sqliteEFModel::outStr(userComs);

    userComs = userModel.table("COMPANY").select().findAlls();
    sqliteEFModel::outStr(userComs);

    sqliteEFCols maxId = userModel.table("COMPANY").select("max(ID) as maxid").find();
    sqliteEFModel::outStr(maxId);

    cout << userModel.table("COMPANY").select().where(userComs[0], "ID").where_or(userComs[0],"NAME,ADDRESS").toSqlStr().c_str() << "\n";

}